package net.zjitc.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author admin
 * @Date 2019/4/3 10:30
 */
public class Test {
    public static void main(String[] args) {
        HashMap<Integer, Student> hashMap = new HashMap<Integer, Student>();
        Student s1 = new Student(1, "张三");
        Student s2 = new Student(2, "李斯");
        Student s3 = new Student(3, "王午");
        hashMap.put(s1.getId(), s1);
        hashMap.put(s2.getId(), s2);
        hashMap.put(s3.getId(), s3);
        System.out.println(hashMap.get(s1.getId()));
        System.out.println("hashmap中所有元素：");
        Set<Integer> set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Integer key = (Integer) iterator.next();
            System.out.println(hashMap.get(key));
        }
        System.out.println("第二种遍历方法");
        Set<Map.Entry<Integer, Student>> set1 = hashMap.entrySet();
        Iterator iterator1 = set1.iterator();
        while (iterator1.hasNext()){
            Map.Entry<Integer,Student> entry=(Map.Entry<Integer, Student>) iterator1.next();
            Integer key=entry.getKey();
            Student value=hashMap.get(key);
            System.out.println(value);
        }
    }
}
